package com.sentrix.auto.printercenter.infrastructure.agentserver.dto;

import cn.hutool.json.JSONUtil;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

@Getter
@Setter
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class BaseWebSocketResMsg extends Uid {
    private boolean success;
    private String result;


    public BaseWebSocketResMsg(String uid, String result) {
        this(uid, true, result);

        if (StringUtils.isNotBlank(result)) {
            try {
                Map<String, Object> obj = JSONUtil.toBean(result, Map.class);
                if (obj.containsKey("error")) {
                    this.success = false;
                }
            } catch (Exception e) {
                log.error("result is not json:{}", e.getMessage());
            }
        }
    }

    public BaseWebSocketResMsg(String uid, Boolean success, String result) {
        super(uid);
        this.success = success;
        this.result = result;
    }
}
